using UnityEngine;
using System.Collections;

public class AntiCamp : MonoBehaviour {
	public GameObject swarm;
	public float minDistance = 5;
	public bool debug = true;
	public float timeLimit = 5;
	
	private GameObject swarmInstantiate;
	private float timer;
	private Vector3 lastPosition;
	private Vector3 lastLastPosition;
	private bool antiCamp =false;
	// Use this for initialization
	void Start () {
		if(transform.parent != null){
			lastPosition = transform.parent.position;	
		}
	
	}
	
	// Update is called once per frame
	void Update () {
		if(swarmInstantiate == null){
			antiCamp = false;	
		}
		if(transform.parent != null){
			timer += Time.deltaTime;
			if(timer > timeLimit){
				timer = 0;
				lastLastPosition = lastPosition;
				lastPosition = transform.parent.position;
			}
			if(Vector3.Distance(lastLastPosition, transform.parent.position) < minDistance){
				if(!antiCamp){
					if(debug)Debug.Log ("Spawning Anti-Camp Swarmer");
					GameObject clone;
					clone = (Network.Instantiate(swarm, transform.parent.position, transform.parent.rotation, 0)) as GameObject;
					clone.SetActive(true);
					clone.GetComponent<AntiCampSwarm>().informationFill(transform.parent.gameObject);
					swarmInstantiate = clone;
					antiCamp = true;
					
				}
				
			}
			
		}
	}
	
}
